package com.example.lanouhn.firstapp.activity.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.lanouhn.firstapp.R;
import com.example.lanouhn.firstapp.activity.activity.AboutOurActivity;
import com.example.lanouhn.firstapp.activity.activity.AddressManagementActivity;
import com.example.lanouhn.firstapp.activity.activity.BrowseHistoryActivity;
import com.example.lanouhn.firstapp.activity.activity.CollectionStoreActivity;
import com.example.lanouhn.firstapp.activity.activity.CooperateActivity;
import com.example.lanouhn.firstapp.activity.activity.DisclaimerActivity;
import com.example.lanouhn.firstapp.activity.activity.FeedbackActivity;
import com.example.lanouhn.firstapp.activity.activity.HelpActivity;
import com.example.lanouhn.firstapp.activity.activity.LoginActivity;
import com.example.lanouhn.firstapp.activity.activity.MainActivity;
import com.example.lanouhn.firstapp.activity.activity.MyCollectionGoodsActivity;
import com.example.lanouhn.firstapp.activity.activity.MyOrdersActivity;
import com.example.lanouhn.firstapp.activity.activity.MyVirtualOrdersActivity;
import com.example.lanouhn.firstapp.activity.activity.MyVouchersActivity;
import com.example.lanouhn.firstapp.activity.model.PersonCenter;
import com.example.lanouhn.firstapp.activity.utils.HttpUtils;
import com.example.lanouhn.firstapp.activity.utils.PersonCenterJson;
import com.example.lanouhn.firstapp.activity.utils.SPUtils;
import com.squareup.picasso.Picasso;


/**
 * 个人中心页面
 */

public class MineFragment extends Fragment {

    //个人中心的关于我们
    private TextView tv_aboutOur;
    //个人中心的免责声明
    private TextView tv_disclaimer;
    //个人中心的商务合作
    private TextView tv_cooperate;
    //个人中心的帮助中心
    private TextView tv_help;

    //个人中心二级页面

    //我的订单
    private LinearLayout myOrders;
    //我的虚拟订单
    private LinearLayout myVirtualOrders;
    //我的代金券
    private LinearLayout myVouchers;
    //我的收藏商品
    private LinearLayout myCollectionGoods;
    //我的收藏店铺
    private LinearLayout myBookmark;
    //地址管理
    private LinearLayout addressManagement;
    //浏览历史
    private LinearLayout browseHistory;
    //意见反馈
    private LinearLayout feedback;
    //注销
    private TextView tv_cancellation;
    //隐藏标题栏
    private LinearLayout ll_mineCenter;
    private ImageView mineCenter;
    private boolean isImageView = true;
    //隐藏标题栏的跳转
    private RelativeLayout rl_homePage;
    private RelativeLayout rl_classify;
    private RelativeLayout rl_shoppingCart;

    //头像
    private ImageView avator;
    //昵称
    private TextView user_name;
    //积分
    private TextView point;
    //预存款
    private TextView pre_deposit;
    //充值卡
    private TextView rechargeableCard;
    //登录标识
    private String key;
    //个人信息类包括昵称、头像等
    private PersonCenter personCenter;
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == 102&&resultCode ==100){
            Intent it = new Intent(getActivity(), MainActivity.class);
            it.putExtra("symbol",4);
            startActivity(it);
        }else {
            getActivity().finish();
        }
    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.minefragment,null);

        //获取key
        key = (String) SPUtils.get(getActivity(),"log_key","");


        avator = (ImageView) view.findViewById(R.id.iv_headPortrait);
        user_name = (TextView) view.findViewById(R.id.user_name);
        point = (TextView) view.findViewById(R.id.point);
        pre_deposit = (TextView) view.findViewById(R.id.pre_deposit);
        rechargeableCard = (TextView) view.findViewById(R.id.rechargeableCard);




        //找到控件
        initView(view);
        //监听事件
        initEvent();
        //获取网络数据
        getDataFromWeb();
        return view;

    }

    private void getDataFromWeb() {

        new Thread(new Runnable() {
            @Override
            public void run() {
                String url = "http://www.niaolema.com/mobile/index.php?act=member_index";
                String params = "key=" + key;
                String result = HttpUtils.doPost(url,params);

                if(!result.equals("") && !result.equals("null")){

                   personCenter = PersonCenterJson.getPersonCenter(result);

                    if(!personCenter.equals("") && !personCenter.equals("null")){

                        handler.sendEmptyMessage(1);
                    }else {
                        handler.sendEmptyMessage(0);

                    }
                }else {
                    handler.sendEmptyMessage(0);
                }

            }
        }).start();





    }

    private Handler handler = new Handler(){

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch(msg.what){
                case 0:
                    Toast.makeText(getActivity(),"请求失败...",Toast.LENGTH_SHORT).show();
                break;

                case 1:
                    Picasso.with(getActivity()).load(personCenter.getAvator()).into(avator);
                    user_name.setText(personCenter.getUser_name());
                    point.setText(personCenter.getPoint());
                    pre_deposit.setText(personCenter.getPredepoit());
                    rechargeableCard.setText(personCenter.getAvailable_rc_balance());


                break;

            }
        }
    };


    //绑定控件
    private void initView(View view) {


        ll_mineCenter = (LinearLayout) view.findViewById(R.id.ll_mineCenter);
        mineCenter = (ImageView) view.findViewById(R.id.mineCenter);

        rl_homePage = (RelativeLayout) view.findViewById(R.id.rl_homePage);
        rl_classify = (RelativeLayout) view.findViewById(R.id.rl_classify);
        rl_shoppingCart = (RelativeLayout)view.findViewById(R.id.rl_shoppingCart);



        tv_aboutOur = (TextView) view.findViewById(R.id.mine_aboutOur);
        tv_disclaimer = (TextView) view.findViewById(R.id.mine_disclaimer);
        tv_cooperate = (TextView) view.findViewById(R.id.mine_cooperate);
        tv_help = (TextView) view.findViewById(R.id.mine_help);
        tv_cancellation = (TextView) view.findViewById(R.id.mine_cancellation);

        //个人中心二级页面 绑定控件id
        myOrders = (LinearLayout) view.findViewById(R.id.myOrders);
        myVirtualOrders = (LinearLayout) view.findViewById(R.id.myVirtualOrders);
        myVouchers = (LinearLayout) view.findViewById(R.id.myVouchers);
        myCollectionGoods = (LinearLayout) view.findViewById(R.id.myCollectionGoods);
        myBookmark = (LinearLayout) view.findViewById(R.id.myBookmark);
        addressManagement = (LinearLayout) view.findViewById(R.id.addressManagement);
        browseHistory = (LinearLayout) view.findViewById(R.id.browseHistory);
        feedback = (LinearLayout) view.findViewById(R.id.feedback);

    }

    //监听事件
    private void initEvent() {

        //关于我们--的点击事件
        tv_aboutOur.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), AboutOurActivity.class);
                startActivity(intent);
            }
        });

        //免责声明--点击事件
        tv_disclaimer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), DisclaimerActivity.class);
                startActivity(intent);
            }
        });
        //商务合作--点击事件
        tv_cooperate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), CooperateActivity.class);
                startActivity(intent);
            }
        });
        //帮助中心--点击事件
        tv_help.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), HelpActivity.class);
                startActivity(intent);
            }
        });
        //注销--点击事件
        tv_cancellation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //点击注销事件  将系统保存在手机中的key值移除
                SPUtils.remove(getActivity(),"log_key");
                Intent intent = new Intent(getActivity(), LoginActivity.class);

                startActivity(intent);
                startActivityForResult(intent,102);

            }
        });

        //个人中心二级页面 点击事件
        myOrders.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), MyOrdersActivity.class);
                startActivity(intent);


            }
        });


        myVirtualOrders.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), MyVirtualOrdersActivity.class);
                startActivity(intent);
            }
        });
        myVouchers.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(),MyVouchersActivity.class);
                startActivity(intent);
            }
        });
        myCollectionGoods.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), MyCollectionGoodsActivity.class);
                startActivity(intent);
            }
        });
        myBookmark.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), CollectionStoreActivity.class);
                startActivity(intent);
            }
        });
        addressManagement.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), AddressManagementActivity.class);
                startActivity(intent);
            }
        });
        browseHistory.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), BrowseHistoryActivity.class);
                startActivity(intent);
            }
        });

        feedback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), FeedbackActivity.class);
                startActivity(intent);
            }
        });

        //隐藏标题栏
        mineCenter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(isImageView){
                    ll_mineCenter.setVisibility(View.VISIBLE);
                    isImageView = false;
                }else {
                    ll_mineCenter.setVisibility(View.GONE);
                    isImageView = true;

                }
            }
        });

        //标题栏首页按钮监听事件
        rl_homePage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), MainActivity.class);
                intent.putExtra("symbol", 1);
                startActivity(intent);
            }
        });
        //标题栏分类按钮监听事件
        rl_classify.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), MainActivity.class);
                intent.putExtra("symbol", 2);
                startActivity(intent);
            }
        });
        //标题栏购物车按钮监听事件
        rl_shoppingCart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), MainActivity.class);
                intent.putExtra("symbol", 3);
                startActivity(intent);
            }
        });
    }


}
